from flask import Flask, request, render_template

'''
Flask获取请求参数
'''
app = Flask(__name__)

# URL查询参数
@app.route('/data')
def test_data():
    print(request.args)
    print(request.args.get('a'), request.args.get('b'))
    print(request.headers)
    return 'success'


# 获取URL参数为json字符串的数据
@app.route('/data_json')
def test_json():
    print(request.data)
    import json
    print(json.loads(request.data))

    return 'success'


# 获取URL请求附带的cookie
@app.route('/data_cookie')
def test_cookie():
    print(request.cookies)
    print(request.cookies.get('token'))

    return 'success'


# form表单参数
@app.route('/data_form', methods=['GET', 'POST'])
def test_form():
    print(request.form)
    print(request.form.get('username'), request.form.get('password'))

    return render_template("test_form.html")


if __name__ == "__main__":
    app.run()